package com.leoman.indexapi.service.Impl;

import com.leoman.creator.dao.CreatorDao;
import com.leoman.creator.entity.Creator;
import com.leoman.creatordynamic.dao.CreatorDynamicDao;
import com.leoman.creatordynamic.dao.DynamicDao;
import com.leoman.creatordynamic.entity.CreatorDynamic;
import com.leoman.creatordynamic.entity.Dynamic;
import com.leoman.creatordynamic.entity.vo.CreatorDynamicListVo;
import com.leoman.creatordynamic.entity.vo.CreatorDynamicVo;
import com.leoman.creatordynamic.entity.vo.CreatorListPlusVo;
import com.leoman.entity.Constant;
import com.leoman.exception.CreatorNotFindException;
import com.leoman.exception.ParamsErrorException;
import com.leoman.exception.UserNotFindException;
import com.leoman.exception.UserStatusErrorException;
import com.leoman.indexapi.service.CreatorDynamicApiService;
import com.leoman.user.dao.UserAttentionDao;
import com.leoman.user.dao.UserInfoDao;
import com.leoman.user.entity.UserInfo;
import com.leoman.utils.DateUtils;
import com.leoman.utils.ImageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 主创动态接口实现
 * Created by tuyh on 2016/6/12.
 */
@Service
public class CreatorDynamicApiServiceImplApi implements CreatorDynamicApiService {

    @Autowired
    private CreatorDynamicDao creatorDynamicDao;

    @Autowired
    private DynamicDao dynamicDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private CreatorDao creatorDao;

    @Autowired
    private UserAttentionDao userAttentionDao;

    @Override
    public List<CreatorDynamicVo> iFindList(Long creatorId, String time) throws CreatorNotFindException {
        return getCreatorDynamicVoList(creatorId, time);
    }

    @Override
    public List<CreatorDynamicVo> iFindList2(Long creatorId, String time) throws CreatorNotFindException {
        if (StringUtils.isNotEmpty(time)) {
            return getCreatorDynamicVoList2(creatorId, time);
        } else {
            // 获取最新时间
            List<String> list = dynamicDao.findListByCreatorId(creatorId);
            if (null == list || list.size() == 0) {
                return new ArrayList<>();
            }

            return getCreatorDynamicVoList2(creatorId, list.get(list.size() - 1));
        }
    }

    @Override
    public CreatorListPlusVo iFindList(Long userId) throws ParamsErrorException, UserNotFindException, UserStatusErrorException {
        CreatorListPlusVo creatorListPlusVo = new CreatorListPlusVo();
        List<CreatorDynamicListVo> list1 = new ArrayList<>();
        List<CreatorDynamicListVo> list2 = new ArrayList<>();
        List<CreatorDynamicListVo> list3 = new ArrayList<>();
        CreatorDynamicListVo creatorDynamicListVo;

        // 活动推荐
        List<Creator> creatorList = creatorDao.iFindList();
        for (CreatorDynamicVo creatorDynamicVo : changeCreatorList(creatorList)) {
            creatorDynamicListVo = new CreatorDynamicListVo();
            creatorDynamicListVo.setType(1);
            creatorDynamicListVo.setUserId(creatorDynamicVo.getUserId());
            creatorDynamicListVo.setCreatorId(creatorDynamicVo.getCreatorId());
            creatorDynamicListVo.setName(creatorDynamicVo.getName());
            creatorDynamicListVo.setAvater(creatorDynamicVo.getAvater());
            creatorDynamicListVo.setIsAttention((null == userId || null == userAttentionDao.findOneInfo(userId, creatorDynamicVo.getUserId())) ? 0 : 1);

            list1.add(creatorDynamicListVo);
        }

        // 艺人
        List<UserInfo> userInfoList = userInfoDao.iFindListByIsCreator(Constant.IF_CREATOR_YES_PLUS);
        for (CreatorDynamicVo creatorDynamicVo : changeCreatorList2(userInfoList)) {
            creatorDynamicListVo = new CreatorDynamicListVo();
            creatorDynamicListVo.setType(2);
            creatorDynamicListVo.setUserId(creatorDynamicVo.getUserId());
            creatorDynamicListVo.setCreatorId(creatorDynamicVo.getCreatorId());
            creatorDynamicListVo.setName(creatorDynamicVo.getName());
            creatorDynamicListVo.setAvater(creatorDynamicVo.getAvater());
            creatorDynamicListVo.setIsAttention((null == userId || null == userAttentionDao.findOneInfo(userId, creatorDynamicVo.getUserId())) ? 0 : 1);

            list2.add(creatorDynamicListVo);
        }

        // 主创
        List<UserInfo> userInfoList1 = userInfoDao.iFindListByIsCreator(Constant.IF_CREATOR_YES);
        for (CreatorDynamicVo creatorDynamicVo : changeCreatorList2(userInfoList1)) {
            creatorDynamicListVo = new CreatorDynamicListVo();
            creatorDynamicListVo.setType(3);
            creatorDynamicListVo.setUserId(creatorDynamicVo.getUserId());
            creatorDynamicListVo.setCreatorId(creatorDynamicVo.getCreatorId());
            creatorDynamicListVo.setName(creatorDynamicVo.getName());
            creatorDynamicListVo.setAvater(creatorDynamicVo.getAvater());
            creatorDynamicListVo.setIsAttention((null == userId || null == userAttentionDao.findOneInfo(userId, creatorDynamicVo.getUserId())) ? 0 : 1);

            list3.add(creatorDynamicListVo);
        }

        creatorListPlusVo.setList1(list1);
        creatorListPlusVo.setList2(list2);
        creatorListPlusVo.setList3(list3);

        return creatorListPlusVo;
    }

    private List<CreatorDynamicVo> changeCreatorList2(List<UserInfo> userInfoList) {
        List<CreatorDynamicVo> list = new ArrayList<>();
        CreatorDynamicVo creatorDynamicVo;

        for (UserInfo userInfo : userInfoList) {
            creatorDynamicVo = new CreatorDynamicVo();
            creatorDynamicVo.setCreatorId(userInfo.getCreator().getId());
            creatorDynamicVo.setUserId(userInfo.getId());
            creatorDynamicVo.setName(userInfo.getNickname());
            creatorDynamicVo.setAvater(ImageUtil.clearUploadUrl(userInfo.getAvater()));

            list.add(creatorDynamicVo);
        }

        return list;
    }

    private List<CreatorDynamicVo> changeCreatorList(List<Creator> creatorList) {
        List<CreatorDynamicVo> list = new ArrayList<>();
        CreatorDynamicVo creatorDynamicVo;
        UserInfo userInfo;

        for (Creator creator : creatorList) {
            userInfo = userInfoDao.findOneByCreatorId(creator.getId());
            creatorDynamicVo = new CreatorDynamicVo();
            creatorDynamicVo.setCreatorId(creator.getId());
            creatorDynamicVo.setUserId(userInfo.getId());
            creatorDynamicVo.setName(userInfo.getNickname());
            creatorDynamicVo.setAvater(ImageUtil.clearUploadUrl(userInfo.getAvater()));

            list.add(creatorDynamicVo);
        }

        return list;
    }

    private List<CreatorDynamicVo> getCreatorDynamicVoList(Long creatorId, String date) {
        List<CreatorDynamicVo> dynamicVoList = new ArrayList<>();
        List<CreatorDynamic> list;

        if (null == creatorId) {
            if (StringUtils.isNotEmpty(date)) {
                list = creatorDynamicDao.findListByDate(date);
            } else {
                list = creatorDynamicDao.findListByDatePlus();
            }
        } else {
            if (StringUtils.isNotEmpty(date)) {
                list = creatorDynamicDao.findListByCreatorIdAndDate(creatorId, date);
            } else {
                list = creatorDynamicDao.findListByCreatorIdPlus(creatorId);
            }
        }

        for (CreatorDynamic creatorDynamic : list) {
            dynamicVoList.add(getCreatorDynamicVo(creatorDynamic));
        }

        return dynamicVoList;
    }

    private CreatorDynamicVo getCreatorDynamicVo(CreatorDynamic creatorDynamic) {
        UserInfo userInfo = creatorDynamic.getUserInfo();
        CreatorDynamicVo creatorDynamicVo = new CreatorDynamicVo();
        creatorDynamicVo.setId(creatorDynamic.getId());
        creatorDynamicVo.setCreatorId(userInfo.getCreator().getId());
        creatorDynamicVo.setUserId(userInfo.getId());
        creatorDynamicVo.setName(userInfo.getNickname());
        creatorDynamicVo.setAvater(ImageUtil.clearUploadUrl(userInfo.getAvater()));

        // 如果当前系统时间在晚上12点~次日早上6点之间，所有主创的信息将全部返回正在休息
        try {
            String nowDay = DateUtils.dateToString(new Date(), "yyyy-MM-dd");
            Date startTime = DateUtils.stringToDate(nowDay + " 00:00:00", "yyyy-MM-dd hh:mm:ss");
            Date endTime = DateUtils.stringToDate(nowDay + " 06:00:00", "yyyy-MM-dd hh:mm:ss");
            Date nowTime = new Date();
            if (nowTime.after(startTime) && nowTime.before(endTime)) {
                creatorDynamicVo.setStatus("正在休息");
            } else {
                // 如果为8，代表是手动输入的信息
                if (creatorDynamic.getStatus() == 8) {
                    creatorDynamicVo.setStatus(creatorDynamic.getThing());
                } else {
                    creatorDynamicVo.setStatus(creatorDynamic.getStatus().toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return creatorDynamicVo;
    }

    private List<CreatorDynamicVo> getCreatorDynamicVoList2(Long creatorId, String date) {
        List<CreatorDynamicVo> dynamicVoList = new ArrayList<>();
        CreatorDynamicVo creatorDynamicVo;
        List<Dynamic> list;

        if (null == creatorId) {
            if (StringUtils.isNotEmpty(date)) {
                list = dynamicDao.findListByDate(date);
            } else {
                list = dynamicDao.findListByDatePlus();
            }
        } else {
            if (StringUtils.isNotEmpty(date)) {
                list = dynamicDao.findListByCreatorIdAndDate(creatorId, date);
            } else {
                list = dynamicDao.findListByCreatorIdPlus(creatorId);
            }
        }

        UserInfo userInfo;

        for (Dynamic dynamic : list) {
            creatorDynamicVo = new CreatorDynamicVo();
            userInfo = userInfoDao.findOneByCreatorId(dynamic.getCreator().getId());
            creatorDynamicVo.setId(dynamic.getId());
            creatorDynamicVo.setCreatorId(dynamic.getCreator().getId());
            creatorDynamicVo.setUserId(userInfo.getId());
            creatorDynamicVo.setName(userInfo.getNickname());
            creatorDynamicVo.setAvater(ImageUtil.clearUploadUrl(userInfo.getAvater()));
            creatorDynamicVo.setStatus(dynamic.getStatus());
            creatorDynamicVo.setTime(dynamic.getTime());

            dynamicVoList.add(creatorDynamicVo);
        }

        return dynamicVoList;
    }
}
